package org.jarraiders.musiclibrary.client.view;

import org.jarraiders.musiclibrary.client.presenter.ArtistsPresenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class ArtistsIcon extends Composite implements HasText, HasMouseOverHandlers, ArtistsPopupPanel.Listener {
	private ArtistsPopupPanel popupPanel;
	private static ArtistsIconUiBinder uiBinder = GWT.create(ArtistsIconUiBinder.class);
	private ArtistsPresenter presenter;
	
	interface ArtistsIconUiBinder extends UiBinder<Widget, ArtistsIcon> {
	}

	@UiField
	Label artistLabel;

	public ArtistsIcon() {
		popupPanel = new ArtistsPopupPanel();
		artistLabel = new Label();
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public ArtistsIcon(String label) {
		popupPanel = new ArtistsPopupPanel();
		artistLabel = new Label(label);
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public ArtistsIcon(String label, int index, ArtistsPresenter presenter) {
		popupPanel = new ArtistsPopupPanel(index, presenter);
		artistLabel = new Label(label);
		initWidget(uiBinder.createAndBindUi(this));
	}
	public ArtistsIcon(int index, ArtistsPresenter presenter) {
		popupPanel = new ArtistsPopupPanel(index, presenter);
		artistLabel = new Label();
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setText(String text) {
		artistLabel.setText(text);
	}

	public String getText() {
		return artistLabel.getText();
	}

	public void setPopupVisible() {

		popupPanel.setVisible(true);
		popupPanel.setPopupPosition(this.getAbsoluteLeft() + 50, this.getAbsoluteTop() + 10);
		popupPanel.show();
	}

	public void setPopupUnvisible() {
		popupPanel.setVisible(false);		
		popupPanel.hide();
	}
	
	public ArtistsPopupPanel getPopupPanel(){
		return popupPanel;
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler){
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	public ArtistsPresenter getPresenter() {
		return presenter;
	}

	public void setPresenter(ArtistsPresenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void onDelete() {
		setPopupUnvisible();		
	}
}
